'use strict';

/* Controllers */
// signin controller
app.controller('DistributionController', ['$rootScope', '$scope', '$state', 'StoreService', 'BaseInfoService', 'dbUtils', function ($rootScope, $scope, $state, storeService, baseInfoService, dbUtils) {
    //init
    baseInfoService.getDelivery().then(function (response) {
        if (response.code == 0) {
            $scope.dis = response.data;
            $scope.dis.delivery = response.data.deliveryType;
            delete $scope.dis.deliveryType;
            if (!response.data.deliveryPrice && !response.data.startDeliveryPrice) {
                $scope.freeDistribution = true;
            }
            if(response.data.startDeliveryPrice || response.data.startDeliveryPrice==0){
                $scope.freeForFull = 1;
            }
        }
    }, function (fail) {

    });

    $scope.setFreeCheck = function(){
        $scope.freeDistribution = !($scope.freeDistribution);
    }

    $scope.submit = function () {
        if ($scope.dis.delivery == 0) {
            $scope.dis = {delivery: 0};
        }
        if($scope.freeDistribution){
            delete $scope.dis.deliveryPrice;
            delete $scope.dis.startDeliveryPrice;
            delete $scope.dis.freeShip;
        }
        if(!$scope.freeForFull){
            delete $scope.freeShip;
        }
        //提交表单
        storeService.setDistribution($scope.dis).then(function(response){
            if(response.code == 0){
                dbUtils.success("提交成功！","提示");
            }else{
                dbUtils.warning(response.message,"提示");
            }
        });
    }
}])
;